package com.fei.jvmtest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MyClassLoader extends ClassLoader {

	public synchronized Class loadClass(String name,InputStream stream, boolean resolve) throws ClassNotFoundException {

		Class c = null;

		byte[] data = getClassData(stream);
		if (data == null)
			throw new ClassNotFoundException();
		c = defineClass(name, data, 0, data.length);

		if (resolve)
			resolveClass(c);
		return c;
	}

	private byte[] getClassData(InputStream stream) {
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1)
				out.write(b, 0, n);
			stream.close();
			out.close();
			return out.toByteArray();

		} catch (IOException e) {
		}
		return null;
	}
}
